package cn.hp.crm.filter;

import cn.hutool.core.util.StrUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Struct;

//  用来拦截非登录用户
public class UserFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

//        从session获取登录情况
        String user = (String) request.getSession().getAttribute("user");
        if (StrUtil.isEmpty(user)){  // null  ""
            response.sendRedirect("/login.html"); // 没有登录用户 重定向到登录页面
        }else {
            chain.doFilter(req, resp);  // 登录过用户 放行
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
